/**
 *@description 用户操作页面js
 *@author XieXing
 *@date 2020/8/18 22:46
 */
var table, form, layer, laydate, layedit, selectM, selectN, multiSelect;//当前页面全局定义 TODO 继续优化
var module = {
    /**
     *@description url集合
     *@author XieXing
     *@date 2020/8/18 22:45
     */
    URL: {
        getDataListUrl: '/gyeducation/api/getDataList', //获取教辅填报数据接口
        getFileDataListUrl: "/gyeducation/api/getFileDataList", //文件数据接口
        downloadUrl: function (path) { //临时路径
            return "/file/download?filePath=C:/Users/18359/Desktop/book/download20200202" + path;
        }
    },
    /**
     *@description 初始化
     *@author XieXing
     *@date 2020/8/18 22:46
     */
    init: function () {
        layui.use(['form', 'table', 'layer', 'laydate', 'layedit', 'upload'], function () {
            table = layui.table;
            form = layui.form;
            layer = layui.layer;
            laydate = layui.laydate;
            var pageData = page.pageData();
            table.render({
                elem: '#datagrid',
                id: 'datagrid',
                url: module.URL.getDataListUrl,
                method: "GET",
                cols: [[
                    {type: 'radio', fixed: 'left'},
                    {type: 'numbers', title: '序号', fixed: "left"},
                    {
                        field: 'schoolName',
                        title: '学校名称',
                        width: '20%'
                    },
                    {
                        field: 'countyName',
                        title: '所属区县',
                        width: '10%'
                    },
                    {
                        field: 'subjectName',
                        title: '学科',
                        width: '10%'
                    },
                    {
                        field: 'classes',
                        title: '班级',
                        width: '15%'
                    },
                    {
                        field: 'grade',
                        title: '年级',
                        width: '10%'
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        width: '20%'
                    },
                    {
                        field: '',
                        title: '操作',
                        width: '10%',
                        templet: function (d) {
                            var str = "<button class='layui-btn layui-btn-normal' type='button' lay-event='add'>教辅详情</button>";
                            return str;
                        }
                    }
                ]],
                height: pageData.tableHeight,
                limit: pageData.limit,
                limits: pageData.limits,
                page: true,
                response: {
                    statusCode: 200
                },
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.stateInfo, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                },
                done: function (res, curr, count) {
                    var perPageMax = $(".layui-laypage-limits select option:last-child").val();//每页最大条数
                    page.tableLoadAll(count, perPageMax);//加载全部
                }
            });


            /**
             *@description 点击查询
             *@author XieXing
             *@date 2020/7/30 11:34
             */
            $("#btnSearch").on("click", function () {
                var qm = {};
                $("#queryForm").serializeArray().forEach(function (each) {
                    qm[each.name] = each.value;
                });
                var cur = parseInt($(".layui-laypage-skip input").val()) || 1;//当前页
                var limit = parseInt($(".layui-laypage-limits select").val()) || 10;//当前每页条数
                table.reload('datagrid', {
                    where: qm,
                    page: {curr: cur, limit: limit}
                });
                return false;
            });

            /**
             *@description 重置
             *@author XieXing
             *@date 2020/8/16 18:08
             */
            $("#btnReset").click(function () {
                $("#queryForm")[0].reset();
                $("#btnSearch").trigger("click");//触发查询操作
            });


            //监听单元格事件
            table.on('tool(datagrid)', function (obj) {
                if (obj.event === 'add') {
                    module.edit_model("教辅填报详情", module.URL.getFileDataListUrl, obj.data);
                }
            });
        });
    },

    /**
     *@description 编辑弹出层
     *@author XieXing
     *@date 2020/8/18 22:00
     */
    edit_model: function (modelName, url, rowData) {
        layer.open({
            type: 1,
            area: ['80%', '60%'],
            title: modelName,
            content: $("#dataModel"),
            offset: ['20px'],
            btn: ['提交', '取消'],
            btn1: function (index, layero) {
                return false;
            },
            btn2: function (index, layero) {
                $("#dataModel form")[0].reset();
                layer.closeAll();
                return false;
            },
            cancel: function (layero, index) {
                $("#dataModel form")[0].reset();
                layer.closeAll();
            },
            success: function (layero, index) {
                /**
                 *@description 根据教辅填报id获取教辅填报详情
                 *@author XieXing
                 *@date 2021/2/8 9:54
                 */
                $.post(url, {id: rowData.id}, function (res) {
                    if (res.code == 200) {
                        var data = res.data;
                        var str = "";
                        for (var i = 0; i < data.length; i++) {
                            str += "<fieldset><legend>第" + (i + 1) + "本情况</legend>";
                            str += "<div class=\"layui-form-item\">\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">封面</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <img src=" + module.URL.downloadUrl(data[i].path) + " class=\"jf-img\" alt=\"图片不存在\">\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">侧面</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <img src=" + module.URL.downloadUrl(data[i].path2) + " class=\"jf-img\" alt=\"图片不存在\">\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "            </div>\n" +
                                "            <div class=\"layui-form-item\">\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">采购路径</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <input type=\"text\" class=\"layui-input none-border\" value=" + data[i].buyRoad + " disabled>\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">使用学生数</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <input type=\"text\" class=\"layui-input none-border\" disabled value=" + data[i].studentUseNum + ">\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "            </div>\n" +
                                "<div class=\"layui-form-item\">\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">评价</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <input type=\"text\" class=\"layui-input none-border\" disabled value=" + data[i].evaluateStr + ">\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "                <div class=\"layui-inline\">\n" +
                                "                    <label class=\"layui-form-label\">是否继续使用</label>\n" +
                                "                    <div class=\"layui-input-inline\">\n" +
                                "                        <input type=\"text\" class=\"layui-input none-border\" disabled value=" + data[i].keepStr + ">\n" +
                                "                    </div>\n" +
                                "                </div>\n" +
                                "            </div>";
                            str += "</fieldset>";
                        }
                        $(".layui-form-detail").html(str);
                    } else {
                        layer.alert(res.stateInfo);
                    }
                })
            }
        });
    },
    /**
     *@description 对象回填数据
     *@author XieXing
     *@date 2020/8/17 15:02
     */
    getData: function (data) {
        for (var k in data) {
            $("input[name=" + k + "],select[name=" + k + "],textarea[name=" + k + "]").val(data[k]);
        }
        form.render();
    }
};

module.init();//运行初始化函数